package org.neuedu.fly.service;

import org.apache.ibatis.session.SqlSession;
import org.neuedu.fly.entity.Reply;
import org.neuedu.fly.entity.vo.ResponseEntity;
import org.neuedu.fly.mapper.ReplyMapper;
import org.neuedu.fly.util.SqlSessionUtil;

import java.util.List;

public class ReplyService {
    private SqlSession session = null;
    private ReplyMapper replyMapper = null;
    public ResponseEntity replyList() {
        session = SqlSessionUtil.getSqlSession();
        replyMapper = session.getMapper(ReplyMapper.class);
        return ResponseEntity.data(replyMapper.replyList());
    }
    public ResponseEntity findReplyByUserId(int id) {
        session = SqlSessionUtil.getSqlSession();
        replyMapper = session.getMapper(ReplyMapper.class);
        List<Reply> data = replyMapper.findReplyByUserId(id);
        return ResponseEntity.data(data);
    }

    public ResponseEntity findReplyByPostId(String postId) {
        session = SqlSessionUtil.getSqlSession();
        replyMapper = session.getMapper(ReplyMapper.class);
        List<Reply> data = replyMapper.findReplyByPostId(Integer.parseInt(postId));
        return ResponseEntity.data(data);
    }

    public ResponseEntity reply(String postId, String replyContext, String userId) {
        session = SqlSessionUtil.getSqlSession();
        replyMapper = session.getMapper(ReplyMapper.class);
        boolean flag = replyMapper.reply(Integer.parseInt(postId), replyContext, Integer.parseInt(userId));
        session.commit();
        if (!flag){
            session.rollback();
            return ResponseEntity.error("出错了");
        }
        return ResponseEntity.ok();
    }

    public ResponseEntity delReplyByReplyId(String replyId) {
        session = SqlSessionUtil.getSqlSession();
        replyMapper = session.getMapper(ReplyMapper.class);
        boolean flag = replyMapper.delById(Integer.parseInt(replyId));
        session.commit();
        if (!flag){
            session.rollback();
            return ResponseEntity.error("出错了");
        }
        return ResponseEntity.ok("删除成功！");
    }

    public ResponseEntity delReplyByUserId(int userId) {
        session = SqlSessionUtil.getSqlSession();
        replyMapper = session.getMapper(ReplyMapper.class);
        boolean flag = replyMapper.delByUserId(userId);
        session.commit();
        if (!flag){
            session.rollback();
            return ResponseEntity.error("出错了");
        }
        return ResponseEntity.ok("删完了！");
    }
}
